import axios from 'axios'
// import store from '@/store'
import {Message} from 'element-ui'
import {getReqUrl} from '/public/config/devConfig'
import router from "@/router"
// 创建一个axios实例
const service = axios.create({
    baseURL: getReqUrl('auth'),
    // 请求超时时间
    timeout: 100 * 1000
})

// 请求拦截器
service.interceptors.request.use((config) => {
    // if (store.getters.token) {
    //   config.headers.Authorization = `Bearer ${store.getters.token}`
    // }
    return config
}, (error) => {
    // 失败执行Promise
    return Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(async (response) => {
    const {data, message, success, code} = response.data
    // 如果成功
    if (success) {
        return data
    } else {
        Message({type: 'error', message: message})
        return Promise.reject(new Error(message))
    }
}, (error) => {
    // element-ui弹出错误信息
    Message({type: 'error', message: error.message})
    return Promise.reject(error)
})

export default service
